package com.freshfresh.activity.selfcenter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.BaseActivity;
import com.freshfresh.activity.R;
import com.freshfresh.adapter.PointsDetailAdapter;
import com.freshfresh.utils.FreshConstants;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;

/**
 * 我的积分
 * 
 * @author Administrator
 * 
 */
public class MemberPointsActivity extends BaseActivity {

	private TextView tv_current_points;
	private TextView tv_grand_total_points;
	private TextView tv_total_cost_points;
	private TextView tv_on_way_points;
	private String points;
	private int page = 1;
	protected boolean isFirst = true;
	private RelativeLayout reloadLy;
	private RelativeLayout loadingLy;

	private SharedPreferences sp;
	private List<Map<String, Object>> listmap;
	private ListView lv_points_detail;
	private String userid;
	private String token;
	private ListView listView = null;
	private Dialog dio;
	private RelativeLayout rel_back=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_my_order_points);
		sp = FreshConstants.getUserShared(MemberPointsActivity.this);
		userid = sp.getString("userid", "");
		token = sp.getString("token", "");
		initView();
		getCustomerScoreRequest();
		getPointsDetail();

	}

	/**
	 * 
	 * @Description 获取积分明细
	 * @param 说明参数含义
	 * @return 说明返回值含义
	 */
	private void getPointsDetail() {

		Map<String, String> params = new HashMap<String, String>();
		params.put("customerid", userid);
		params.put("token", token);

		dio = Utils.initProgressDialog(MemberPointsActivity.this);
		dio.show();
		executeRequest(new StringRequest(UrlConstants.getPointsHistory, params,
				new Response.Listener<String>() {

					private PointsDetailAdapter adapter;

					@Override
					public void onResponse(String arg0) {
						dio.dismiss();
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							Map<String, Object> map = (Map<String, Object>) Utils
									.parseJsonStr(arg0).get("data");
							String points = map.get("points_list_total")
									.toString();
							int point = Integer.valueOf(points);
							if (point > 0) {
								List<Map<String, Object>> map2 = (List<Map<String, Object>>) map
										.get("points_list");
								adapter = new PointsDetailAdapter(
										MemberPointsActivity.this, map2);
								listView.setAdapter(adapter);
							}
						}
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						showToast(MemberPointsActivity.this, "获取积分明细失败，"
								+ "请稍候再试");
					}

				}));
	}

	private void getCustomerScoreRequest() {
		Map<String, String> params = new HashMap<String, String>();
		params.put("customerid", userid);
		params.put("token", token);
		System.out.println(userid + "++++++++++" + token);

		executeRequest(new StringRequest(UrlConstants.getCustomerScore, params,
				new Response.Listener<String>() {

					@Override
					public void onResponse(String arg0) {
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							Map<String, Object> map = (Map<String, Object>) Utils
									.parseJsonStr(arg0).get("points");
							
							String pendingPoints = map.get("pending_points")
									.toString(); // 在途积分
							System.out
									.println("+++++++++++++++++++++++++++++++"
											+ pendingPoints);

							String spentPoints = map.get("spent_points")
									.toString();// 花费的积分
							String currentPoints = map.get("current_points")
									.toString();// / 当前可用积分
							String accumulatedPoints = map.get(
									"accumulated_points").toString();// 积分总数
							// String moneyPoints = map.get("money_points")
							// .toString();// 设置当前积分

							// 设置当前积分
							if (TextUtils.isEmpty(currentPoints)
									|| currentPoints.equals("null")) {
								tv_current_points.setText("当前积分: " + "0");
							} else {
								tv_current_points.setText("当前积分: "
										+ currentPoints);
							}

							// // /设置在途积分
							if (TextUtils.isEmpty(pendingPoints)
									|| pendingPoints.equals("null")) {
								tv_on_way_points.setText("0");
							} else {
								tv_on_way_points.setText(pendingPoints);
							}
							// 设置总花费积分
							if (TextUtils.isEmpty(spentPoints)
									|| spentPoints.equals("null")) {
								tv_total_cost_points.setText("0");
							} else {
								tv_total_cost_points.setText(spentPoints);
							}

							// 设置累计积分
							if (TextUtils.isEmpty(accumulatedPoints)
									|| accumulatedPoints.equals("null")) {
								tv_grand_total_points.setText("0");
							} else {
								tv_grand_total_points
										.setText(accumulatedPoints);
							}

						}
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError arg0) {
						showToast(MemberPointsActivity.this, "获取会员积分失败，"
								+ "请稍候再试");
					}
				}));

	}

	private void initView() {

		rel_back = (RelativeLayout) findViewById(R.id.rel_back);
		TextView title_center_text = (TextView) findViewById(R.id.tv_title);
		title_center_text.setVisibility(View.VISIBLE);
		title_center_text.setText("我的积分");

		tv_current_points = (TextView) findViewById(R.id.tv_current_points);
		tv_total_cost_points = (TextView) findViewById(R.id.tv_total_cost_points);
		tv_grand_total_points = (TextView) findViewById(R.id.tv_grand_total_points);
		tv_on_way_points = (TextView) findViewById(R.id.tv_on_way_points);
		listView = (ListView) findViewById(R.id.listview);
		
		rel_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				MemberPointsActivity.this.finish();
			}
		});


	}

}
